package com.kuang.demo.config.wx;


import com.kuang.demo.mapper.wx.WXAuthMapper;
import org.apache.shiro.authc.*;

import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Authoriz extends AuthorizingRealm {

    @Autowired
    WXAuthMapper wxAuthMapper;

    @Override
    //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MyToken token = (MyToken)authenticationToken;
        if("wx".equals(token.getType())){
        String username = token.getUsername();
        String password = wxAuthMapper.selectPasswordByName(username);
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, this.getName());
        return simpleAuthenticationInfo;}
        else{
            String username = token.getUsername();
            String password = wxAuthMapper.selectPasswordByAdmin(username);
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, this.getName());
            return simpleAuthenticationInfo;
        }
    }

    @Override
    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Object primaryPrincipal = principalCollection.getPrimaryPrincipal();

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermission("admin");
        return simpleAuthorizationInfo;
    }

}
